
const { where, CITEXT } = require('sequelize')
const Goods = require('../model/Goods.model')

class Goodservice {
    async createGoods(goods) {
        const res = await Goods.create(goods)
        return res.dataValues
    }
    async updateGoods(id, goods) {
        const res = await Goods.update(goods, { where: { id } })
        console.log(res)
        return res[0] > 0 ? true : false
    }
    async removeGoods(id) {
        const res = await Goods.destroy({ where: { id } })
        return res > 0 ? true : false
    }
    async restoreGoods(id) {
        const res = await Goods.restore({ where: { id } })
        return res > 0 ? true : false
    }
    async findGoods(pageNum, pageSize) {
        const offset = (pageNum - 1) * pageSize
        // 获取总数
        // const count = Goods.count()
        // 获取分页具体数据
        // const rows = await Goods.findAll({ offset: offset, limit: pageSize * 1 })


        const { count, rows } = await Goods.findAndCountAll({ offset: offset, limit: pageSize * 1 })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }

    }
    async findOne(goods_id) {
        const res = await Goods.findOne({ where: { id: goods_id } })
        return res ? res.dataValues : null
    }
}

module.exports = new Goodservice()